﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectCloth.Entity;
using ProjectCloth.FrmUI.Common;

namespace ProjectCloth.FrmUI.ColorCards
{
    public partial class ColorCardTableFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        private List<Product> products = new List<Product>();
        public ColorCardTableFrm()
        {
            InitializeComponent();
            products = db.Products.ToList();
        }
        private void dataGdv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var dataGdv = dataGridView1;
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGdv.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGdv.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGdv.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            var startTime = startDatePiker.Value.Date;
            var endTime = endDatePicker.Value.AddDays(1).Date;
            dataGridView1.AutoGenerateColumns = false;
            var list = GetColorCardsList();
            dataGridView1.DataSource = list;
            quanlitySumTxt.Text = list.Sum(o => o.Quantity).ToString();
            colorQtySumTxt.Text = list.Sum(o => o.ColorQuantity).ToString();
        }
        private List<ColorCard> GetColorCardsList()
        {
            var startTime = startDatePiker.Value.Date;
            var endTime = endDatePicker.Value.AddDays(1).Date;
            var list = (from g in db.ColorCards.Where(o => o.MakeTime > startTime && o.MakeTime < endTime && o.Enabled).ToList()
                        group g by g.ColorCardNo into gc
                        select new ColorCard()
                        {
                            ColorCardNo = gc.Key,
                            ColorQuantity = gc.Sum(o => o.ColorQuantity),
                            Quantity = gc.Sum(o => o.Quantity),
                            CreateTime = startTime,
                            MakeTime = startTime
                        }).ToList();
            var zerolist = (from p in products
                            select new ColorCard()
                            {
                                ColorCardNo = p.ProductNo,
                                ColorQuantity = 0,
                                Quantity = 0,
                                CreateTime = startTime,
                                MakeTime = startTime
                            }).ToList();
            zerolist.ForEach(o =>
            {
                if (list.Count(p => p.ColorCardNo == o.ColorCardNo) <= 0) list.Add(o);
            });
            return list.OrderBy(o => o.ColorCardNo).ToList();
        }
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var sql = GetColorCardsList();
            DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
            if (column != null)
            {
                var list = new List<SaleTableModel>();
                if (column.Name == "ColorCardNo")
                {
                    dataGridView1.DataSource = sql.OrderByDescending(o => o.ColorCardNo).ToList();
                }
                else if (column.Name == "ColorQuantity")
                {
                    dataGridView1.DataSource = sql.OrderByDescending(o => o.ColorQuantity).ToList();
                }
                else if (column.Name == "MakeTime")
                {
                    dataGridView1.DataSource = sql.OrderByDescending(o => o.MakeTime).ToList();
                }
                else if (column.Name == "Quantity")
                {
                    dataGridView1.DataSource = sql.OrderByDescending(o => o.Quantity).ToList();
                }
            }
        }

        private void dataGridView1_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var sql = GetColorCardsList();
            DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
            if (column != null)
            {
                if (column.Name == "ColorCardNo")
                {
                    dataGridView1.DataSource = sql.OrderBy(o => o.ColorCardNo).ToList();
                }
                else if (column.Name == "ColorQuantity")
                {
                    dataGridView1.DataSource = sql.OrderBy(o => o.ColorQuantity).ToList();
                }
                else if (column.Name == "MakeTime")
                {
                    dataGridView1.DataSource = sql.OrderBy(o => o.MakeTime).ToList();
                }
                else if (column.Name == "Quantity")
                {
                    dataGridView1.DataSource = sql.OrderBy(o => o.Quantity).ToList();
                }
            }
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                #region  
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn)
                {
                    var cardId = dataGridView1.CurrentRow.Cells["Id"].Value;
                    if (cardId != null && (int)cardId > 0)
                    {
                        //if (column.Name == "EditBtn")
                        //{
                        //    string productNo = dataGridView1.CurrentRow.Cells["ProductNo"].Value.ToString();
                        //    if (!string.IsNullOrEmpty(productNo))
                        //    {
                        //        var product = new ProductFrm(productNo);
                        //        var result = product.ShowDialog();
                        //        if (result.Equals(DialogResult.Cancel))
                        //        {
                        //            searchBtn_Click(null, null);
                        //        }

                        //    }
                        //}

                        if (column.Name == "Delete")
                        {
                            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                            DialogResult dr = MessageBox.Show("确定要删除该色卡吗?", "删除该色卡", messButton);
                            if (dr == DialogResult.OK)//如果点击“确定”按钮
                            {
                                db.ColorCards.Find((int)cardId).Enabled = false;
                                if (db.SaveChanges() > 0)
                                {
                                    MessageBox.Show("删除成功！");
                                    searchBtn_Click(null, null);
                                }
                                else
                                {
                                    MessageBox.Show("删除失败！");
                                }

                            }

                        }
                    }
                }
                #endregion
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            Helper.ExportDgv(dataGridView1, "色卡统计");
        }
    }
}
